package xyz.yolin.utils;

import com.google.common.base.CaseFormat;
import com.google.common.collect.Maps;
import org.springframework.cglib.beans.BeanMap;
import xyz.yolin.pojo.Admin;
import xyz.yolin.pojo.Survey;

import java.util.HashMap;
import java.util.Map;

//实现bean和map之间转换
public class BeanMapUtils {

    //将对象转换成map
    public static <T>Map<String,Object> beanToMap(T bean){
        Map<String,Object> map=new HashMap<String, Object>();
        if(bean!=null){
            BeanMap beanMap = BeanMap.create(bean);
//            System.out.println("工具类BeanMap生成的值为空的Map"+beanMap);
            for (Object key:beanMap.keySet()){
                map.put(key+"",beanMap.get(key));
            }
        }
        return map;
    }
    public static <T> Map<String,Object> beanToMapForUpdate(T bean){
        System.out.println("-------------");
        Map<String, Object> map = new HashMap<String, Object>();
        System.out.println("传过来的bean"+bean);
        if(bean != null){
            BeanMap beanMap = BeanMap.create(bean);
//            System.out.println("工具类BeanMap生成的值为空的Map"+beanMap);
            for(Object key:beanMap.keySet()){
//                map.put(CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.LOWER_CAMEL,"update"+key),beanMap.get(key));
                map.put("update"+upperFirstLatter(key+""),beanMap.get(key));
            }
        }
        System.out.println("针对更新操作特定返回的map"+map);
        return map;
    }
//    将首字母大写的方法
    public static String upperFirstLatter(String letter){
        char[] chars = letter.toCharArray();
        if(chars[0]>='a' && chars[0]<='z'){
            chars[0] = (char) (chars[0]-32);
        }
        return new String(chars);
    }
    public static void main(String[] args) {
      //  创建Admin对象，并将其转为map
        Admin admin = new Admin();
        admin.setId(2);
        admin.setAccount("1000");
        Map<String, Object> map = beanToMap(admin);
        System.out.println(map);

        Survey survey = new Survey();
        Map<String, Object> map2 = beanToMapForUpdate(survey);
        System.out.println(map2);
    }


}
